@Cofe ?= {}
mime = Cofe.MIMETypes
utils = Cofe.utils

lowercaseFirstLetter = (str) ->
  if str is undefined
    return ""
  else return str[0].toLowerCase() + str[1..str.length-1]  


# CLASS - Represents a COFEWS's Link, which contains: 
# mediatype - type of stream to be returned when called.
# rel - relation to the current object (e.g. self, download)
# url - relative path of the url.
class Link  
  constructor : (mediatype, rel, uri, params)->
    @mediatype = mediatype
    @rel = lowercaseFirstLetter(rel)
    @uri = uri
    @url = if params isnt undefined and params isnt null and params.length > 0 then uri + "?" + $.param(params, true) else uri
    @params = params
    

  # Create a link from a <link> xml element.
  Link.fromJson = (data) ->
    new Link(data.mediatype, data.rel, data.uri)

  # Create a link from a folder path (e.g. {cofe}\obj)
  Link.fromPath = (folderPath, params) ->
    if params is undefined then params = {path : folderPath } else params.path = folderPath
    new Link(mime.EntryList, "folder", "/api/parse", params)
   
  # Create a link from search parameter (e.g. root:{cofe}\obj subdir:true filetype:dll)
  Link.fromSearch = (searchParam, params) ->
    url = "/api/search?filterStr=" + searchParam
    new Link(mime.EntryList, "search", url, searchParam, params)    

  Link.extractLinkpath = (path) ->  
    return undefined if path.rel isnt "folder"
    return undefined if path.param is undefined || path.param is ""
    return Link.fromPath(utils.extractFilepath(path.param))

Cofe.Link = Link